<?php
/**
 * Created by PhpStorm.
 * User: 冯轲
 * Date: 2017-12-13
 * Time: 11:59
 */
namespace app\home\controller;

use think\Controller;
use think\Db;
use think\Session;


class Collections extends Controller
{
  public  function getadd()
  {
      if(!empty(Session::get('userinfo')['user_id']))
      {
          $request=$this->request;
          $res=$request->param();
          $res['goods_id']=$res['id'];
          $res['user_id']=Session::get('userinfo')['user_id'];
          $res['create_time']=time();
          unset($res['id']);
          unset($res['action']);
          $ress=Db::table('tp_collections')->where('user_id',$res['user_id'])
              ->where('goods_id',$res['goods_id'])->find();
          if($ress)
          {
              $data=[
                  'status' => 1,
                  'msg' => '你已经收藏过该商品，不能重复收藏该商品',
              ];
              return $data;
          }
          $result=Db::table('tp_collections')->insert($res);
          if($result){
              //返回数据，进行判断是否添加成功
              $data=[
                  'status' => 0,
                  'msg' => '收藏成功',
              ];
          }else{
              $data=[
                  'status' => 1,
                  'msg' => '收藏失败',
              ];
          };
          return json($data);
      }else{
          $data=[
              'status' => 1,
              'msg' => '请登录',
          ];
          return $data;
      }
  }

  public function getIndex()
  {
    $user_id=Session::get('userinfo')['user_id'];
    $goods_datas=Db::table('tp_collections')->alias('tc')
        ->field('tc.id,tc.goods_id,tg.goods_price,tg.id,tg.goods_name,goods_pic')
        ->join('tp_goods tg','tc.goods_id=tg.id')
        ->where('user_id',$user_id)
        ->select();
    $this->assign('goods_datas',$goods_datas);
    return $this->fetch('collections/index');
  }

}